package com.springmvc.config;

import com.springmvc.interceptor.MyInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/12/9 21:14
 * <p>
 * springMvc只扫描Controller：子容器
 * useDefaultFilters = false：禁用默认的过滤规则
 */
@EnableWebMvc
@ComponentScan(value = "com.springmvc", includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}, useDefaultFilters = false)
@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
    //定制视图解析器
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        // 默认所有的页面都从/WEB-INF/下获取
        registry.jsp("/WEB-INF/views/", ".jsp");
        super.configureViewResolvers(registry);
    }

    //静态资源访问
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
    }
}
